package com.zk.erp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zk.erp.entity.JsonResult;
import com.zk.erp.entity.User;
import com.zk.erp.entity.UserRole;
import com.zk.erp.service.IUserRoleService;
import com.zk.erp.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zk
 * @since 2021-04-12
 */
@RestController
public class UserController {
    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private IUserService userService;
    //分页查询所有的User
    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public JsonResult findPage(@RequestParam(value = "curr",required = false,defaultValue = "1")Long curr){
        try {
            Page<User> tPage = new Page<>(curr, 10);
            IPage<User> page = userService.findPage(tPage);
            return new JsonResult(1,page);
        }catch (Exception e){
            return new JsonResult(0,null);
        }
    }
    //删除User
    @RequestMapping(value = "/user/{userId}",method = RequestMethod.DELETE)
    public JsonResult deleteUser(@PathVariable("userId") String userId){
        List<UserRole> list = userRoleService.list(new QueryWrapper<UserRole>().lambda().eq(UserRole::getUserId, userId));
        if (list!=null||list.size()!=0){
            list.forEach(userRole -> {
                userRoleService.remove(new QueryWrapper<UserRole>().lambda().eq(UserRole::getUserId,userRole.getUserId()));
            });
        }
        userService.removeById(userId);
        return new JsonResult(1,null);
    }
}
